package com.lushuai.jiubeidanci.util;

import android.content.Context;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lushuai.jiubeidanci.model.Word;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class WordDataUtil {
    public static List<Word> loadWords(Context context) {
        try {
            // 从 assets 目录读取 JSON 文件
            InputStream is = context.getAssets().open("words.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            String json = new String(buffer, StandardCharsets.UTF_8);

            // 解析 JSON
            Gson gson = new Gson();
            Type type = new TypeToken<WordResponse>(){}.getType();
            WordResponse response = gson.fromJson(json, type);
            return response.words;
        } catch (IOException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    private static class WordResponse {
        List<Word> words;
    }
} 